package cn.xn.hrms.api.controller;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.req.PositionReqDTO;
import cn.xn.hrms.domain.service.PositionService;
import cn.xn.hrms.domain.utils.ResultVo;
import cn.xn.hrms.infrastructure.dataobject.PositionDO;
import cn.xn.hrms.infrastructure.dataobject.PositionSelectDO;
import io.swagger.annotations.ApiOperation;

/**
 * @author ysz
 */
@RestController
public class PositionController {

    @Resource
    PositionService positionService;

    /**
     * 分页查询岗位列表
     *
     * @param page
     * @param size
     * @param departmentId
     * @param positionName
     * @return
     */
    @GetMapping("/position/list")
    @ApiOperation("分页查询岗位列表")
    public PageInfo<PositionDO> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                     @RequestParam(value = "departmentId", defaultValue = "0") Integer departmentId,
                                     @RequestParam(value = "positionName", required = false) String positionName) {
        return positionService.findAll(page, size, departmentId, positionName);
    }

    /**
     * 岗位列表
     *
     * @return
     */
    @GetMapping("/position/all")
    @ApiOperation("岗位列表")
    public List<PositionDO> all() {
        return positionService.all();
    }


    /**
     * 根据部门id查询部门岗位
     *
     * @param departmentId
     * @return
     */
    @GetMapping("/position/select/{departmentId}")
    @ApiOperation("根据部门id查询部门岗位")
    public List<PositionSelectDO> selectList(@PathVariable Integer departmentId) {
        return positionService.findSelect(departmentId);
    }

    /**
     * 新增岗位
     *
     * @param Position
     * @return
     */
    @PostMapping("/position")
    @ApiOperation("新增岗位")
    @RequiresRoles("admin")
    public ResultVo insert(@RequestBody @Valid PositionReqDTO Position) {
        positionService.insert(Position);
        return ResultVo.success();
    }

    /**
     * 修改岗位
     *
     * @param Position
     * @param id
     * @return
     */
    @PutMapping("/position/{id}")
    @ApiOperation("修改岗位")
    @RequiresRoles("admin")
    public ResultVo update(@RequestBody PositionReqDTO Position, @PathVariable Integer id) {
        positionService.updateById(id, Position);
        return ResultVo.success();
    }

    /**
     * 删除岗位（支持批量）
     *
     * @param id
     * @return
     */
    @DeleteMapping("/position/delete")
    @ApiOperation("删除岗位（支持批量）")
    @RequiresRoles("admin")
    public ResultVo deleteIdIn(@RequestBody Set<Integer> id) {
        positionService.deleteByIdIn(id);
        return ResultVo.success();
    }
}
